export default {

	methods: {

		//获取工单详情
		async GetOrderDetailData(userPhone, WoGuid, token) {
			let result = await this.workorderdetailservice.woItemAsync(userPhone, WoGuid, token);
			if (result.RetBool == true) {
				let data = JSON.parse(result.RetObject);
				this.orderDetailData = data;
				//console.log(this.orderDetailData);
			} else {
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
			uni.hideLoading();
		},
		
		//原因列表
		async ReasonCodeListAsync(UserPhone, ReasonCodeType, token) {
			let result = await this.workorderdetailservice.ReasonCodeListAsync(UserPhone, ReasonCodeType, token);
			console.log(result);
			if (result.RetBool == true) {
				let data = JSON.parse(result.RetObject);
				console.log(data);
				return data;
			} else {
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
				return null;
			}
			return null;
		},
		
		
		
		//上门组件打开时
		VisitPoPupOpen() {
			//let data = {};
			this.VisitData.UserId = this.UserInfo.mobile;
			this.VisitData.WOGUID = this.orderDetailData.woid;
			this.VisitData.Longitude = this.currentLongitude;
			this.VisitData.Latitude = this.currentLatitude;
			this.VisitData.Location = this.userPosition;
			this.VisitData.NewAddress = this.orderDetailData.street;
			//this.VisitData.Milestone = this.distance;
			this.VisitData.IsLocationSuccess = this.IsLocationSuccess;
			this.VisitData.Remark = this.vitisRemark;
			//this.VisitData = data;
			console.log(this.VisitData);
			this.$refs.visitPoPup.openPoPup();
		},
		
		//获取当前用户位置的经纬度
		async GetUserLocation(callback) {
			//debugger
			//uni.openSetting({});
			let me = this;
			me.IsLocationSuccess = false;
			uni.getLocation({
				type: 'gcj02',
				success: async function(res) {
					me.currentLongitude = res.longitude;
					me.currentLatitude = res.latitude;
		
					let result = await me.getLocationAddress(me.currentLongitude, me.currentLatitude);
					me.userPosition = result.result.address;
					me.getGPS(me.orderDetailData.displayadress, me.UserInfo.RetId);
					me.IsLocationSuccess = true;
					
				},
				fail: function(res) {
					if (res.errMsg == "getLocation:fail auth deny") {
						uni.showModal({
							title: '是否授权当前位置',
							content: '需要获取您的地理位置，请确认授权',
							success: function(tip) {
								if (tip.confirm) {
									uni.openSetting({
										success(data) {
											if (data.authSetting["scope.userLocation"] === true) {
												uni.getLocation({
													type: 'gcj02',
													async success(res) {
														me.currentLongitude = res.longitude;
														me.currentLatitude = res.latitude;
														let result = await me.getLocationAddress(me.currentLongitude, me.currentLatitude);
														me.userPosition = result.result.address;
														me.getGPS(me.orderDetailData.displayadress, me.UserInfo.RetId);
														me.IsLocationSuccess = true;
		
													}
												});
											}
										},
										fail(error) {
											wx.showToast({
												title: '调用授权窗口失败',
												icon: 'none',
												duration: 1000
											})
										}
									})
								}
							}
						})
					}
				}
			});
		},
		
		//根据经纬度查出地址名称
		async getLocationAddress(lon, lat) {
			let params = {
				location: `${lat},${lon}`
			}
			let result = await this.mapApiService
				.geoMultipParamMapAsync(this.qqMapServiceUrl, "/ws/geocoder/v1", params);
			return result;
		},
		
		//根据地址名称获取经纬度
		async getGPS(address, token) {
			let result = await this.mapApiService.getGPS(address, token);
			console.log(result);
			if (result.RetBool == true) {
				let data = JSON.parse(result.RetObject);
				console.log(data);
				this.accountLongitude = data.result.location.lng;
				this.accountLatitude = data.result.location.lat;
				this.calcMapDistance(this.currentLongitude, this.currentLatitude, this.accountLongitude, this.accountLatitude)
			}
		},
		
		//根据经纬度计算距离
		async calcMapDistance(currentLongitude, currentLatitude, accountLongitude, accountLatitude) {
			var form = new Array(currentLatitude, currentLongitude);
			var to = new Array(accountLatitude, accountLongitude);
			let params = {
				mode: "driving",
				from: form.toString(),
				to: to.toString(),
			};
			let data = await this.mapApiService.geoMultipParamMapAsync(this.qqMapServiceUrl, "/ws/distance/v1", params);
			console.log(data);
			if (data != null) {
				//距离-米
				let distance = data.result.elements[0].distance;
				//this.distance = distance / 1000;
				this.VisitData.Milestone = distance / 1000;
				console.log(this.distance);
				this.VisitPoPupOpen();
			}
		},
		
		//工单-上门
		async WoFieldServiceAsync(WOFieldService, token) {
			let result = await this.workorderdetailservice.woFieldServiceAsync(WOFieldService, token);
			console.log(result);
			if (result.RetBool == true) {
				uni.showToast({
					title: "已确定上门",
					icon: "none"
				});
				this.$refs.visitPoPup.closePoPup();
			} else {
				uni.showToast({
					title: result.RetMsg,
					icon: "none"
				});
			}
		},
	}
}
